package com.kding.deviceunique;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import com.kding.deviceunique.utils.Encryption;
import com.kding.deviceunique.utils.FileUtil;


/**
 * 根据设备名称以及设备型号和驱动长度rom仿造一个IMEI
 * we make this look like a valid IMEI
 * 1.每次先检测文件里面是否生成了一个加密的文件（）
 */
public class UUIDUtils {
    public final static String TAG = "UUID";

    /**
     * 默认不去读取用户mac地址 隐私条款禁止使用Mac地址
     */
    @SuppressLint({"MissingPermission", "HardwareIds"})
    public static String getUniqueID(Context context) {
        String serial = null;
        String wlanMac = null;
        String imei = null; //IMEI码
        String uuid = null;
        //设置一个随机数防止冲突
        String randomNum = null;

        //we make this look like a valid IMEI
        String deviceId = context.getApplicationContext().getPackageName() +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10
                + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
                + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
                + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
                + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
                + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
                + Build.USER.length() % 10; // 13 digits;


        //通过系统权限获取手机信息获取到一个IMEI随即串
        try {
            //待权限开启之后才可以获取到设备的IMEI唯一识别码 如果不开启权限的时候 IMEI == null
            TelephonyManager telephonyManager = (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                if (Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 28) {//android 6.0-android8.1
                    imei = telephonyManager.getDeviceId(0);
                } else if (Build.VERSION.SDK_INT >= 28) {
                    imei = Settings.System.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)
                            + Build.TIME + Build.USER + Build.HOST + Build.FINGERPRINT;
                } else {
                    imei = telephonyManager.getDeviceId();
                }
            }

            //如果获取到IMEI了则直接返回用户的IMEI
            if (!TextUtils.isEmpty(imei)) {
                uuid = Encryption.md5(imei);
                Log.e(TAG, "获取到IMEI ===》" + imei);
                return FileUtil.saveUUID(context, uuid);
            }
            if (imei == null) {
                //这里做一层保护， 小米手机有一个特殊授权，授权可以不给
                throw new Exception("用户不授权或则因为设置导致为null情况，设置权限返回null");
            }
            Log.e(TAG, "imei ===》" + imei);
        } catch (SecurityException e) {
            //防止部分手机没有授权抛出异常，
            if (TextUtils.isEmpty(imei)) {
                imei = Settings.System.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID)
                        + Build.TIME + Build.USER + Build.HOST + Build.FINGERPRINT;
            }
        } catch (Exception e) {
            randomNum = System.currentTimeMillis() + "";
            Log.e(TAG, "设备IMEI获取为null 随机生成一个串码：" + randomNum);
        }
        uuid = Encryption.md5(randomNum + deviceId + serial + wlanMac + imei);
        return FileUtil.saveUUID(context, uuid);
    }


}
